package com.example.bookmaster.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.text.ParseException;

/**
 * Created by fois-vn on 4/3/14.
 */
public class DatabaseHandle extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "BookDB";
    private static final String book_id = "book_id";
    private static final String book_title = "book_title";
    private static final String author = "author_name";
    private static final String publisher = "publisher";
    private static final String publication_day = "publication_day";
    private static final String[] Columns = {"book_id", "book_title", "author_name", "publisher", "publication_day"};

    public DatabaseHandle(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_BOOK_TABLE = "create table Books(" +
                "book_id TEXT PRIMARY KEY," +
                "book_title TEXT," +
                "author_name TEXT," +
                "publisher TEXT," +
                "publication_day TEXT" +
                ")";
        sqLiteDatabase.execSQL(CREATE_BOOK_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS Books");
        this.onCreate(sqLiteDatabase);
    }

    public boolean addBook(Book book) {
        SQLiteDatabase db = this.getWritableDatabase();
        try {
//            Log.d("addBook", book.toString());

            ContentValues contentValues = new ContentValues();
            contentValues.put(book_id, book.getBook_id());
            contentValues.put(book_title, book.getBook_title());
            contentValues.put(author, book.getAuthor_name());
            contentValues.put(publisher, book.getPublisher());
            contentValues.put(publication_day, book.getPublication_day());

            db.insert("Books", null, contentValues);
            return true;
        } catch (Exception ex) {
            return false;
        } finally {
            db.close();
        }
    }

    //update book
    public boolean updateBook(Book book) {
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(book_title, book.getBook_title());
            contentValues.put(author, book.getAuthor_name());
            contentValues.put(publisher, book.getPublisher());
            contentValues.put(publication_day, book.getPublication_day());

            int i = db.update("Books", contentValues, "book_id = ?", new String[]{book.getBook_id()});
            db.close();

            return i == 1;
        } catch (Exception e) {
            return false;
        }
    }

    //delete book
    public boolean deleteBook(Book book) {
        SQLiteDatabase db = this.getWritableDatabase();
        try {
//            Log.i("delete", book.getBook_id());
            db.delete("Books", book_id + " = ?", new String[]{book.getBook_id()});
            return true;
        } catch (Exception e) {
            return false;
        } finally {
            db.close();
        }
    }

    public Book getBook(String id) throws ParseException {
        try {
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.query("Books", Columns, "book_id = ?", new String[]{id}, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();
            Book book = new Book();
            book.setBook_id(cursor.getString(0));
            book.setBook_title(cursor.getString(1));
            book.setAuthor_name(cursor.getString(2));
            book.setPublisher(cursor.getString(3));
            book.setPublication_day(cursor.getString(4));
            return book;
        } catch (Exception e) {
            return null;
        }


    }
}
